<?php
namespace App\Traits;
use App\Http\Controllers\MailController;
use App\Models\Manage;
use App\Models\Member;
use Illuminate\Support\Facades\Cache;

trait CheckMessageNotice
{
    // 系统配置
    private $Setting = null;
    // 审核通知类型
    private $_type = '';
    // 操作上传的用户id
    private $_handle_member_id = 0;

    /**
     * 发送系统审核通知管理员员工
     * @param string $_type 发送通知类型 media=素材库 coupon=卡券
     * @param int $_handle_member_id 操作上传的用户id
     * @return string
     */
    public function sendCheckNotice($_type,$_handle_member_id)
    {
        $this->Setting = $this->getConfig();
        $_is_check = false;
        $this->_type = $_type;
        $this->_handle_member_id = $_handle_member_id;
        if($this->_type) {
            $_is_check = $this->Setting->media->is_check;
        } else if($this->_type == 'coupon') {
            $_is_check = $this->Setting->coupon_check;
        }
        if(!$this->testSendCheckNotice($_is_check)) return;
        // 是否开启了相关的审核功能
        if($_is_check) {
            // 获取一个审核管理员会员id
            $_manage_member_id = $this->getManage();
            if($_manage_member_id) {
                // 是否设置了通知模板消息id
                if($this->Setting->message->await_check_tpl) {
                    // 设置了模板消息id
                    if(!$this->sendTplMessage($_manage_member_id)) {
                        $this->sendEmail($_manage_member_id);
                    }
                } else {
                    // 没有设置模板消息id,邮箱通知
                    $this->sendEmail($_manage_member_id);
                }
            }
        }
    }

    /**
     * 发送模板消息
     * @param int $_member_id 管理员的会员id
     * @return bool
     */
    private function sendTplMessage($_member_id)
    {
        $Wechat = $this->wechat();
        $_keyword1 = '';
        $_remark = '';
        if($this->_type == 'media') {
            $_keyword1 = '用户上传素材';
            $_remark = '请登录后台处理审核素材';
        } else if($this->_type == 'coupon') {
            $_keyword1 = '用户编辑卡券';
            $_remark = '请登录后台处理审核卡券';
        }
        $_tpl_result = $Wechat->template_message->send([
            'touser' => Member::where('id',$_member_id)->value('openid'),
            'template_id' => $this->Setting->message->await_check_tpl,
            'data' => [
                'first' => [
                    'value' => $this->Setting->website_name .'待审核通知'
                ],
                'keyword1' => [
                    'value' => $_keyword1
                ],
                'keyword2' => [
                    'value' => Member::where('id',$this->_handle_member_id)->value('nickname')
                ],
                'keyword3' => [
                    'value' => date('Y-m-d H:i:s')
                ],
                'remark' => [
                    'value' => $_remark
                ]
            ]
        ]);
        if($_tpl_result['errcode'] == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送邮件通知
     * @param $_member_id
     */
    private function sendEmail($_member_id)
    {
        $_type_string = '';
        $_remark = '';
        if($this->_type == 'media') {
            $_type_string = '用户上传素材';
            $_remark = '请登录后台处理审核素材';
        } else if($this->_type == 'coupon') {
            $_type_string = '用户编辑卡券';
            $_remark = '请登录后台处理审核卡券';
        }
        $_nickname = Member::where('id',$this->_handle_member_id)->value('nickname');
        $_date = date('Y-m-d H:i:s');
        $Mail = new MailController();
        $Mail->send(
            Manage::where('member_id',$_member_id)->value('email'),
            $this->Setting->website_name . '待审核通知',
            "<p>类型：{$_type_string}</p><p>上传用户：{$_nickname}</p><p>上传时间：{$_date}</p><p>{$_remark}</p>",
            true
        );
    }

    /**
     * 获取一个通知的员工会员id
     */
    private function getManage()
    {
        $_count = Manage::where('identity','check')->count();
        if($_count <= 0) return false;
        $_offset =mt_rand(0,$_count-1);
        return Manage::where('identity','check')
            ->offset($_offset)
            ->limit(1)
            ->value('member_id');
    }

    /**
     * 检测是否模板消息通知，防止短时间内频繁模板消息通知
     * @param string $_key 缓存键值
     * @return bool
     */
    public function testSendCheckNotice($_key)
    {
        $_key .= $_key.'_await_check_tpl';
        if(Cache::get($_key)) return false;
        Cache::put($_key,1,60);
        return true;
    }
}
